package architecture.demo13;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private static final String SERVER_IP = "localhost";
    private static final int SERVER_PORT = 8888;

    public void start() {
        try {
            Scanner scanner = new Scanner(System.in);
            Socket socket = new Socket(SERVER_IP, SERVER_PORT);

            ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());

            System.out.println("欢迎使用通讯录系统！");
            System.out.println("请输入要执行的操作：");
            System.out.println("1. 添加联系人");
            System.out.println("2. 搜索联系人");
            System.out.println("3. 删除联系人");
            System.out.println("4. 更新联系人");
            System.out.println("5. 退出");
            while (true) {


                String command = scanner.nextLine();

                switch (command) {
                    case "1":
                        addContact(scanner, outputStream, inputStream);
                        break;
                    case "2":
                        searchContacts(scanner, outputStream, inputStream);
                        break;
                    case "3":
                        removeContact(scanner, outputStream, inputStream);
                        break;
                    case "4":
                        updateContact(scanner, outputStream, inputStream);
                        break;
                    case "5":
                        System.out.println("已退出通讯录系统。");
                        socket.close();
                        return;
                    default:
                        System.out.println("无效的操作，请重新输入。");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addContact(Scanner scanner, ObjectOutputStream outputStream, ObjectInputStream inputStream) throws IOException {
        System.out.println("请输入联系人的姓名：");
        String name = scanner.nextLine();

        System.out.println("请输入联系人的地址：");
        String address = scanner.nextLine();

        System.out.println("请输入联系人的电话号码：");
        String phoneNumber = scanner.nextLine();

        Contact contact = new Contact(name, address, phoneNumber);
        Message message = new Message(Operation.ADD, contact);
        sendMessage(outputStream, message);

        // 接收服务端的响应消息
        Message response = receiveMessage(inputStream);
        System.out.println("服务端响应：\n" + response.getData());
        System.out.println("----------------------");
    }

    private void searchContacts(Scanner scanner, ObjectOutputStream outputStream, ObjectInputStream inputStream) throws IOException {
        System.out.println("请输入关键词：");
        String keyword = scanner.nextLine();

        Message message = new Message(Operation.SEARCH, keyword);
        sendMessage(outputStream, message);

        // 接收服务端的响应消息
        Message response = receiveMessage(inputStream);
        System.out.println("服务端响应：\n" + response.getData());
        System.out.println("----------------------");
    }

    private void removeContact(Scanner scanner, ObjectOutputStream outputStream, ObjectInputStream inputStream) throws IOException {
        System.out.println("请输入要删除的联系人的姓名：");
        String name = scanner.nextLine();

        Message message = new Message(Operation.REMOVE, new Contact(name, "", ""));
        sendMessage(outputStream, message);

        // 接收服务端的响应消息
        Message response = receiveMessage(inputStream);
        System.out.println("服务端响应：\n" + response.getData());
        System.out.println("----------------------");
    }

    private void updateContact(Scanner scanner, ObjectOutputStream outputStream, ObjectInputStream inputStream) throws IOException {
        System.out.println("请输入要修改的联系人的姓名：");
        String name = scanner.nextLine();

        System.out.println("请输入新的联系人信息：");
        System.out.println("请输入联系人的姓名：");
        String newName = scanner.nextLine();

        System.out.println("请输入联系人的地址：");
        String newAddress = scanner.nextLine();

        System.out.println("请输入联系人的电话号码：");
        String newPhoneNumber = scanner.nextLine();

        Contact oldContact = new Contact(name, "", "");
        Contact newContact = new Contact(newName, newAddress, newPhoneNumber);
        Message message = new Message(Operation.UPDATE, new Contact[]{oldContact, newContact});
        sendMessage(outputStream, message);

        // 接收服务端的响应消息
        Message response = receiveMessage(inputStream);
        System.out.println("服务端响应：\n" + response.getData());
        System.out.println("----------------------");
    }

    private void sendMessage(ObjectOutputStream outputStream, Message message) throws IOException {
        outputStream.writeObject(message);
        outputStream.flush();
    }

    private Message receiveMessage(ObjectInputStream inputStream) throws IOException {
        try {
            return (Message) inputStream.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();
    }
}
